package com.axin.oss.controller;

import cn.hutool.core.util.ObjectUtil;
import com.axin.common.core.annotation.RepeatSubmit;
import com.axin.common.core.domain.web.AjaxResult;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.core.exception.ServiceException;
import com.axin.common.core.utils.JsonUtils;
import com.axin.common.core.validate.QueryGroup;
import com.axin.common.security.base.BaseController;
import com.axin.oss.api.domain.SysOss;
import com.axin.oss.api.dto.SysOssDTO;
import com.axin.oss.api.vo.SysOssVo;
import com.axin.oss.service.ISysOssService;
import com.axin.oss.service.ISysOssConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author axin
 * @Date 2021/11/4 10:58
 **/
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/oss")
public class SysOssController extends BaseController {


    private final ISysOssService ossService;


    /**
     * 查询OSS对象存储列表
     */
    @PreAuthorize("@ss.hasPermi('oss:oss:list')")
    @GetMapping("/list")
    public TableDataInfo<SysOssVo> list(@Validated(QueryGroup.class) SysOssDTO dto) {
        return ossService.queryPageList(dto);
    }


    /**
     * 上传OSS对象存储
     */
    @ApiOperation("上传OSS对象存储")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "java.io.File", required = true),
    })
    @PreAuthorize("@ss.hasPermi('oss:oss:upload')")
    @RepeatSubmit
    @PostMapping("/upload")
    public AjaxResult<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        SysOss oss = ossService.upload(file);
        Map<String, String> map = new HashMap<>(2);
        map.put("url", oss.getUrl());
        map.put("fileName", oss.getFileName());
        return AjaxResult.success(map);
    }

    /**
     * 博客内容图片上传
     * @param upload 图片
     * @param responseType 响应格式 json
     * @return  前端图片回显格式
     * {
     *      fileName: "01e32cc90dd34191a09a4f866dd6ab31.jpg",
     *      uploaded: 1,
     *      url: "https://ckeditor.com/apps/ckfinder/userfiles/files/01e32cc90dd34191a09a4f866dd6ab31.jpg"
     * }
     */
    @PostMapping("/uploadImage")
    public Map<String, Object> uploadImage(@RequestPart("upload") MultipartFile upload, @RequestParam(required = false, name = "responseType") String responseType) {
        if (ObjectUtil.isNull(upload)) {
            throw new ServiceException("上传文件不能为空");
        }
        SysOss oss = ossService.upload(upload);
        Map<String, Object> map = new HashMap<>(3);
        // 前端图片回显格式

        map.put("fileName", oss.getFileName());
        map.put("uploaded", 1);
        map.put("url", oss.getUrl());

        return map;
    }

    /**
     * 删除OSS对象存储
     */
    @ApiOperation("删除OSS对象存储")
    @PreAuthorize("@ss.hasPermi('system:oss:remove')")
    @DeleteMapping("/{ossIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable Long[] ossIds) {
        return toAjax(ossService.deleteWithValidByIds(Arrays.asList(ossIds), true) ? 1 : 0);
    }

}
